﻿using MCPlatform.Models;
using MCPlatform.UI.Components;
using MCPlatform.UI.Controls.Converts;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace MCPlatform.ViewModels.Shell
{
    public class UserLoginViewModel : ViewModelBase
    {
        public event Action<string>? LoginFail;
        public event Action<string>? LoginSuccess;

        public CommandBase LoginCommand { get; set; }
        public CommandBase LogoutCommand { get; set; }

        public UserLoginViewModel()
        {
            LoginCommand = new CommandBase(Login);

            LogoutCommand = new CommandBase(Logout);
        }

        private void Login(Object o)
        {
            //获取账号和密码信息
            var values = (Object[])o;
            var userID = values[0].ToString();
            var PasswordBox = (PasswordBox)values[1];
            var password = PasswordBox.Password;

            if (userID == "" || password == "")
            {
                Toast.Show("账号或密码为空", targetWindow: ToastWindow.MainWindow);
                LoginFail?.Invoke("账号或密码为空");
                return;
            }
            else if (userID.Equals("Admin") && password.Equals("123"))
            {
                Toast.Show("登录成功", targetWindow: ToastWindow.MainWindow);
                LoginSuccess?.Invoke(userID);
                this.TryCloseAsync();
                return;
            }
            else if (userID.Equals("Engineer") && password.Equals("111"))
            {
                Toast.Show("登录成功", targetWindow: ToastWindow.MainWindow);
                LoginSuccess?.Invoke(userID);
                this.TryCloseAsync();
                return;
            }
            else if (userID.Equals("Operator"))
            {
                Toast.Show("登录成功", targetWindow: ToastWindow.MainWindow);
                LoginSuccess?.Invoke(userID);
                this.TryCloseAsync();
                return;
            }

            LoginFail?.Invoke("密码错误");
            Toast.Show("密码错误", targetWindow: ToastWindow.MainWindow);

        }

        private void Logout(Object o)
        {
            LoginSuccess?.Invoke("Operator");
            this.TryCloseAsync();
        }

        
    }
}
